package com.spica.platform.uaa.server.service.impl;

import com.spica.platform.base.feign.UserFeignClient;
import com.spica.platform.base.model.LoginAppUser;
import com.spica.platform.base.model.ResponseEntity;
import com.spica.platform.common.constant.SecurityConstant;
import com.spica.platform.uaa.server.service.DefaultUserDetailsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.social.security.SocialUserDetails;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 默认后台用户类型插件
 */
@Slf4j
@Service
public class UserDetailServiceImpl implements DefaultUserDetailsService {
    private static final String ACCOUNT_TYPE = SecurityConstant.DEF_ACCOUNT_TYPE;

    @Resource
    private UserFeignClient userFeignClient;

    @Override
    public boolean supports(String accountType) {
        return ACCOUNT_TYPE.equals(accountType);
    }

    @Override
    public UserDetails loadUserByUsername(String username) {
        ResponseEntity<LoginAppUser> loginAppUser = userFeignClient.findLoginAppUserByUsername(username);
        return  loginAppUser.getData();
    }

    @Override
	public SocialUserDetails loadUserByUserId(String userId) throws UsernameNotFoundException {
		ResponseEntity<LoginAppUser> loginAppUser = userFeignClient.findLoginAppUserByUserId(userId);
        return  loginAppUser.getData();
	}
    
    @Override
    public SocialUserDetails loadUserByOpenId(String openId) {
        ResponseEntity<LoginAppUser> loginAppUser = userFeignClient.findLoginAppUserByOpenId(openId);
        return  loginAppUser.getData() ;
    }

    @Override
    public UserDetails loadUserByMobile(String mobile) {
        ResponseEntity<LoginAppUser> loginAppUser = userFeignClient.findLoginAppUserByMobile(mobile);
        return  loginAppUser.getData() ;
    }


	
}
